package main

import (
	"fmt"
	"log"
	"time"
)

func main() {
	simpleTimer()
	//explicit()
	//anonymous()

	//tickerDemo()
}

func simpleTimer() {
	for {
		// 执行定时任务的操作
		fmt.Println("执行定时任务")
		time.Sleep(5 * time.Second) // 暂停 5 秒
	}
}

// 显式
func explicit() {
	timer := time.NewTimer(time.Second * 2)

	<-timer.C
	fmt.Println("mode one: Time out!  and time is ", time.Now())
}

// 匿名
func anonymous() {
	select {
	case <-time.After(time.Second * 2):
		fmt.Println("mode two: Time out! and time is ", time.Now())
	}
}

func afterDemo() {
	log.Println(time.Now())
	<-time.After(1 * time.Second)
	log.Println(time.Now())
}

func tickerDemo() {
	ticker := time.NewTicker(time.Second * 2)
	defer ticker.Stop()
	for range ticker.C {
		fmt.Println("Time Out! and time is ", time.Now())
	}
}
